package com.example.administrator.text;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.io.FileNotFoundException;
import app.BaseActivity;
import app.MyApplication;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import data.SharedPreferencesUtils;
import util.AgentIp;
import util.BitmapUtil;
import util.HomeUtil;
import util.JumpActivityUtils;
import util.LogUtil;
import util.Url;
import view.diaLogView.WeAccountShowImageDialog;

/**
 * 修改个人资料的页面
 */
public class WeInformation1Activity extends BaseActivity {
    private WeAccountShowImageDialog mWeAccountShowImageDialog;
    private String IMAGE_FILE_NAME = "clip_temp.jpg";//选择头像 相机 图片保存的名字
    private String RESULT_PATH = "result_path";
    private String PASS_PATH = "pass_path";
    private  final int CUT = 3;
    private int mPwd;
    private boolean mState = false;  //记录退出是否被点击过

    @Bind(R.id.frame_weinformation1)              FrameLayout mFrameLayout;
    @Bind(R.id.activity_weinformation_head)       ImageView mImageView;                   //头像
    @Bind(R.id.activity_weinformation_tv_name)    TextView mTVName;                      //昵称
    @Bind(R.id.activity_weinformation_tv_phone)   TextView mTVPhone;                    //手机
    @Bind(R.id.activity_weinformation_tv_password)TextView mTVPassword;                //密码
    @Bind(R.id.activity_weinformation_tv_sex)     TextView mTVSEX;                   //性别
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weinformation1);
        ButterKnife.bind(this);
        init();

    }

    private void init() {
        HomeUtil.getHemeUtilNew().getStatusBar(this, mFrameLayout);
        setInformation();
    }

    /**
     * 返回
     */
    @OnClick(R.id.weinformation1_image_about)
    void weinformation(){
        /*Intent intent = new Intent();
        setResult(RESULT_OK, intent);*/
        finish();
        LogUtil.eE("","weinformation");
    }

    /**
     * 设置昵称
     */
    @OnClick(R.id.activity_weinformation_ll2)
    void ll2(){
        JumpActivityUtils.getInstance().jumpModifypassword1(WeInformation1Activity.this, ModifyInformationActivity.NICKNAME);
    }

    /**
     * 设置 手机号
     */
    @OnClick(R.id.activity_weinformation_ll3)
    void ll3(){
        JumpActivityUtils.getInstance().jumpModifypassword1(WeInformation1Activity.this, ModifyInformationActivity.PHONE);
    }

    /**
     * 设置 性别
     */
    @OnClick(R.id.activity_weinformation_ll5)
    void ll5(){
        JumpActivityUtils.getInstance().jumpModifypassword1(WeInformation1Activity.this, ModifyInformationActivity.SEX);
    }

    /**
     * 设置 密码
     */
    @OnClick(R.id.activity_weinformation_ll4)
    void ll4(){
        JumpActivityUtils.getInstance().jumpModifypassword1(WeInformation1Activity.this, ModifyInformationActivity.PWD);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Intent intent = new Intent();
        setResult(RESULT_OK, intent);
        intent.putExtra("type","");
        finish();
        return false;
    }

    /**
     * 头像
     */
    @OnClick(R.id.activity_weinformation_head)
    void weinformtihead_head(){
        mWeAccountShowImageDialog = new WeAccountShowImageDialog(this,"",mBaseImageLoader,null);
        mWeAccountShowImageDialog.show();
    }


    @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode ){
            case 0:
                String type = data.getStringExtra("type");
                String content = data.getStringExtra("content");
                if(type.equals(ModifyInformationActivity.PWD)){
                    mTVPassword.setText("******");
                }else if(type.equals(ModifyInformationActivity.NICKNAME)){
                    mTVName.setText(content);
                }else if(type.equals(ModifyInformationActivity.PHONE)){
                    mTVPhone.setText(content);
                }else if(type.equals(ModifyInformationActivity.SEX)){
                    if(content.equals("1")){
                        mTVSEX.setText("男");
                    }else{
                        mTVSEX.setText("女");
                    }
                }
                break;
            case 1:
                Log.e("pat","头像相册");
                startCropImageActivity(getFilePath(data.getData()));
                break;
            case 2:
                Log.e("pat", "头像相册");
                // 照相机程序返回的,再次调用图片剪辑程序去修剪图片
                startCropImageActivity(Environment.getExternalStorageDirectory() + "/" + IMAGE_FILE_NAME);
                break;
            case 3:
                String path = data.getStringExtra(RESULT_PATH);
                LogUtil.eE("","WeAccountShowImageActivity       "+path);
                Bitmap bitmap = BitmapFactory.decodeFile(path);
                Url.getUrlNew().setWE_PORTRAIT(path);
                mImageView.setImageBitmap(bitmap);
                EventBus.getDefault().post("update_head");
                break;
        }

    }

   /**
     * 设置相关信息
     */
    private void setInformation(){
        final String headUrl = (String) SharedPreferencesUtils.getParam(this, ModifyInformationActivity.WEHEAD,"");     //头像地址
        String nicKname = (String) SharedPreferencesUtils.getParam(this, ModifyInformationActivity.NICKNAME,""); //昵称
        mPwd = (int) SharedPreferencesUtils.getParam(this, ModifyInformationActivity.PWD,1);               //密码
        String phone = (String) SharedPreferencesUtils.getParam(this, HomeUtil.mHUWePhone,"");            //手机
        int sex = (int)SharedPreferencesUtils.getParam(this,ModifyInformationActivity.SEX,1);            //性别
        if(phone.equals("")){
            mTVPhone.setText("未录入");
        }else {
            mTVPhone.setText(phone);
        }

        if(nicKname.equals("")){
            mTVName.setText("未录入");
        }else {
            mTVName.setText(nicKname);
        }

        if(mPwd == 1){
            mTVPassword.setText("******");
        }else if(mPwd == 0){

            mTVPassword.setText("未录入");
        }else {
            mTVPassword.setText("未录入");
        }

        if(sex == 1){
            LogUtil.eE("",sex+"");
            mTVSEX.setText("男");
        }else if(sex == 2){
            mTVSEX.setText("女");
        }else {
            mTVSEX.setText("未录入");
        }

        if(Url.getUrlNew().getWE_PORTRAIT().equals("")){
            AgentIp.newInstance(this).setNetWorkstate2(new AgentIp.OnNetworkStateClickListener() {
                @Override
                public void onClick(boolean state) {
                    if(state){
                        ImageLoader mImageLoader = MyApplication.getApp().getImageLoader();
                        mImageLoader.displayImage(headUrl, mImageView, HomeUtil.getHemeUtilNew().getDisplayImageOptions(true, R.mipmap.weaccount_head));
                    }else {
                        BitmapUtil.newBitmap().showBitmap(WeInformation1Activity.this,headUrl,mImageView, R.mipmap.weaccount_head,null);
                    }
                }
            });
        }else {
            Bitmap bitmap = BitmapFactory.decodeFile(Url.getUrlNew().getWE_PORTRAIT());
            mImageView.setImageBitmap(bitmap);
        }

    }

   /**
     *裁剪图片的Activity
     */
    private void startCropImageActivity(String path) {
        File file = new File(path);
        LogUtil.eE("path",""+path);
        LogUtil.eE("path",""+file.length());
        if(file.length() !=0){
            Intent intent = new Intent(this, ClipImageActivity.class);
            intent.putExtra(PASS_PATH, path);
            startActivityForResult(intent,CUT);
        }else {getToast("请重新选择图片");}

    }

    /**
     * 通过uri获取文件路径
     *
     */
    public String getFilePath(Uri mUri) {
        try {
            if (mUri.getScheme().equals("file")) {
                return mUri.getPath();
            } else {
                return getFilePathByUri(mUri);
            }
        } catch (FileNotFoundException ex) {
            return null;
        }
    }

    // 获取文件路径通过url
    private String getFilePathByUri(Uri mUri) throws FileNotFoundException {
        Cursor cursor = getContentResolver()
                .query(mUri, null, null, null, null);
        cursor.moveToFirst();
        return cursor.getString(1);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mWeAccountShowImageDialog !=null){
            mWeAccountShowImageDialog.dismiss();
        }
    }
}
